class UsersController < ApplicationController
  before_filter :authenticate
  before_filter:list_users,:only =>[:show]
  before_filter :departments_list

 
  def index
    @title ="雇员列表"
    unless params[:search].blank?
           
      search = params[:search]
      @users=[]
    
      search2=search.split(%r(,|;|:))

      search2.each do |s|
        @users += User.search4(s)
      end
      @users.uniq!
    else
      @users = User.search(params[:page])
   # @users = User.paginate :per_page => 20,:page =>1,
    #          :include =>[:department],:order => "last_name asc,first_name asc"
    end
    

    
    #    render :update do |page|
    #      page.replace_html "search_result", :partial => 'search_result' 
    #    end
  
    #	end
    
    #	@users = User.find(:all,:include =>[:departments])
    #	@users_pages,@users = User.paginate(:users)	
  end


  def show
    @title ="雇员详细信息"
    @user = User.find(params[:id])
    @projects = @user.projects
    @productusers= @user.productusers
    @tests_coupons = @user.projectusers.sum(:point)

  end


  def new
    @title ="新增一名雇员"
    @user = User.new

  end
  


  def edit
    @title ="修改雇员信息"
    @user = User.find(params[:id])
  end


  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        flash[:notice] = 'User was successfully created.'
        format.html { 
          if params[:save_next]
            redirect_to("/users/new")
          else
            redirect_to("/users")  
          end
           }
      else
        render :action => "new" 
      end
    end
  end


  def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        flash[:notice] = 'User was successfully updated.'
        format.html { redirect_to(@user) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end


  def destroy
    @user = User.find(params[:id])
    @user.destroy
    page = params[:page] || 1

    respond_to do |format|
      format.html { redirect_to("/users?page=#{page}") }
      format.xml  { head :ok }
    end
  end
  
  def search_users
    
    unless params[:search].blank?
      search = params[:search]
      @users=[]
    
      search2=search.split(%r(,|;|:|，|：|；))
		search2.uniq!
      s3=search2.select {|item| item.strip.size >0}
    #  s3.uniq!

      s3.each do |s|
        @users |= User.search4(s.strip)
      end
  #   @users.uniq!
         
      #    render :update do |page|
      render :partial =>"search_result2"
    else
      @users = User.search(params[:page])
     render :partial =>"search_result"
   # redirect_to("/users")
    #redirect_to :action => 'index'
    end
    #    end
  end
  
  def add_products
    @productuser = Productuser.new
    @user = User.find(params[:id])
    @productusers= @user.productusers
    @categories = Category.find(:all,:order => "id")
        
  end
  
  def find_product
    begin
    @product = Product.find(params[:productuser][:product_id])
    render :update do |page|
      page.replace "productuser_per_cost","<input type='text' style='cursor: text;' readonly='true' value='#{@product.per_cost}' size='5' name='productuser[per_cost]' id='productuser_per_cost'/>"
    end
  rescue
    render :update do |page|
      page.replace "productuser_per_cost","<input type='text' disabled='true' value='0' size='5' name='productuser[per_cost]' id='productuser_per_cost'/>"
    end
  end
  end
  
  def check_user
    # layout => false
    chinese_name = params[:chinese_name]
    unless chinese_name.blank?
      @user = User.find_by_chinese_name(chinese_name)
    end
    
    render :layout => false
  end
  
  def check_email
    # layout => false
    email = params[:email]
    unless email.blank?
      @user = User.find_by_email(email)
    end
    render :layout => false
  end
    
  private
  
  def authenticate
    authenticate_or_request_with_http_basic do |username, password|
      username == "admin" && password == "123456"
    end
  end
  
  def list_users
    @users=User.find(:all,:select =>"english_name,chinese_name,id")
    @users_group = @users.group_by {|u| u.english_name[0..0].upcase}
    @groups=@users_group.keys.select {|g| g != '' && g !='#'}.sort!
  end
  
  def departments_list
    @departments =Department.find(:all,:order => "name ASC")
  end

end
